/************************************************************************************************************************
 Name of the class: DBUtils.java
 
 Author: Saiaswin Saikannan
 
 Role: This class performs CRUD operations with the Google App Engine Datastore using the JDO(Java Data Object) Interface 

 ***********************************************************************************************************************/


package com.request;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DBUtils {

	public static void insert(Request d) throws Exception {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			pm.makePersistent(d);
		} catch (Exception ex) {
			throw ex;
		} finally {
			pm.close();
		}
	}

	public static List<Request> listrequest(String sortorder) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = null;

		query = pm.newQuery(Request.class);
		query.setOrdering(sortorder);
		List<Request> result = (List<Request>) query.execute();

		return result;
	}

	public static Request retrieve(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Request.class, "id == identity");
		q.declareParameters("Long identity");
		List<Request> result = (List<Request>) q.execute(id);
		return result.get(0);
	}

	public static void delete(Key k) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Request r = pm.getObjectById(Request.class, k);
		pm.deletePersistent(r);
	}

}